package com.itheima.response.c_pic;

import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

@WebServlet("/PicServlet")
public class PicServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取文件在服务器的输入流对象
        InputStream is = getServletContext().getResourceAsStream("/img/01.png");

        //2. 获取字节输出流 (指向浏览器端)
        ServletOutputStream os = response.getOutputStream();

        //3. IO流对拷
        IOUtils.copy(is,os);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
